package com.free.compilation.service;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.common.core.utils.DateUtils;
import com.free.common.core.web.page.TableDataInfo;
import com.free.compilation.domain.*;
import com.free.compilation.mapper.ZoneCompilationChapterMapper;
import com.free.compilation.mapper.ZoneCompilationMapper;
import com.free.compilation.mapper.ZoneCompilationStepMapper;
import com.free.system.api.RemoteSystemService;
import com.free.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 合集Service业务层处理
 *
 * @author free
 * @date 2023-07-25
 */
@Service
public class ZoneCompilationService extends ServiceImpl<ZoneCompilationMapper, ZoneCompilation> {
    @Autowired
    private ZoneCompilationMapper zoneCompilationMapper;
    @Autowired
    private ZoneCompilationStepMapper zoneCompilationStepMapper;
    @Autowired
    private ZoneCompilationChapterMapper zoneCompilationChapterMapper;
    @Autowired
    RemoteSystemService systemService;

    /**
     * 查询合集
     *
     * @param id 合集主键
     * @return 合集
     */
    public ZoneCompilation selectZoneCompilationById(Long id) {
        return zoneCompilationMapper.selectById(id);
    }

    /**
     * 查询合集列表
     *
     * @param zoneCompilation 合集
     * @return 合集
     */


    /**
     * 新增合集
     *
     * @param zoneCompilation 合集
     * @return 结果
     */
    public int insertZoneCompilation(ZoneCompilation zoneCompilation) {
        zoneCompilation.setCreateTime(new Date());
        return zoneCompilationMapper.insert(zoneCompilation);
    }

    /**
     * 修改合集
     *
     * @param zoneCompilation 合集
     * @return 结果
     */
    public int updateZoneCompilation(ZoneCompilation zoneCompilation) {
        zoneCompilation.setUpdateTime(DateUtils.getNowDate());
        return zoneCompilationMapper.updateById(zoneCompilation);
    }

    /**
     * 批量删除合集
     *
     * @param ids 需要删除的合集主键
     * @return 结果
     */
    public int deleteZoneCompilationByIds(Long ids) {
        QueryWrapper<ZoneCompilationStep> wrapper = new QueryWrapper<>();
        wrapper.eq("compilation_id" , ids);
        Long count = zoneCompilationStepMapper.selectCount(wrapper);
        Assert.isTrue(count == 0, "还有没有放生的记录哟(●'◡'●)" );
        return zoneCompilationMapper.deleteById(ids);
    }

    /**
     * 删除合集信息
     *
     * @param id 合集主键
     * @return 结果
     */
    public int deleteZoneCompilationById(Long id) {
        return zoneCompilationMapper.deleteById(id);
    }

    public List<ZoneCompilation> selectZoneCompilationList(ZoneCompilation zoneCompilation) {

        QueryWrapper<ZoneCompilation> queryWrapper=new QueryWrapper(zoneCompilation);
        queryWrapper.orderByDesc("id" );
        return this.zoneCompilationMapper.selectList(queryWrapper);
    }

    public int listStep(Long id) {


        return 1;
    }

    public List<CompilationClassify> compilationClassifyAll() {
        QueryWrapper<ZoneCompilation> wrapper = new QueryWrapper<>();
        wrapper.isNull("classify" );//        全部
        List<ZoneCompilation> zoneCompilations = this.baseMapper.selectList(wrapper);
        ArrayList<CompilationClassify> compilationClassifies = new ArrayList<>();
        ArrayList<CompilationClassifySub> subs = new ArrayList<>();
        CompilationClassify classify = new CompilationClassify();
        classify.setFoods(subs);

        classify.setId(0L);
        classify.setName("全部" );
        zoneCompilations.forEach(e -> {
            setCompilations(subs, e);
        });
        compilationClassifies.add(classify);
        extracted(compilationClassifies);

        return compilationClassifies;
    }

    private void extracted(ArrayList<CompilationClassify> compilationClassifies) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType("compilation_type" );
        TableDataInfo list = systemService.list(dictData);
        List<SysDictData> rows = JSONUtil.toList(JSONUtil.toJsonStr(list.getRows()), SysDictData.class);
        rows.forEach(sysDictData -> {
            CompilationClassify classify = new CompilationClassify();
            ArrayList<CompilationClassifySub> subs = new ArrayList<>();
            classify.setFoods(subs);
            classify.setId(sysDictData.getDictCode());
            classify.setName(sysDictData.getDictLabel());
            QueryWrapper<ZoneCompilation> wrapper = new QueryWrapper<>();
            wrapper.eq("classify" , sysDictData.getDictCode());
            wrapper.orderByDesc("id" );
            this.baseMapper.selectList(wrapper).forEach(e -> {
                setCompilations(subs, e);

            });
            compilationClassifies.add(classify);
        });
    }

    private void setCompilations(ArrayList<CompilationClassifySub> subs, ZoneCompilation e) {
        CompilationClassifySub sub = new CompilationClassifySub();
        sub.setCat(e.getId());
        sub.setName(e.getTitle());
        sub.setIcon(e.getCoverImage());
        sub.setKey(e.getId() + "" );
        sub.setDescri(e.getDescri());
        subs.add(sub);
        //添加段落
        QueryWrapper<ZoneCompilationChapter> query = new QueryWrapper();
        query.eq("compilation_id" , e.getId());
        List<CompilationChapterSub> subList = new ArrayList<>();
        zoneCompilationChapterMapper.selectList(query).forEach(chapter -> {
            CompilationChapterSub compilationChapterSub = new CompilationChapterSub();
            compilationChapterSub.setCat(chapter.getId());
            compilationChapterSub.setKey(chapter.getId() + "" );
            compilationChapterSub.setName(chapter.getChapterName());
            subList.add(compilationChapterSub);
        });
        sub.setSubs(subList);
    }
}
